package muse.proxy;
/**
 * CBRScheduler.java
 * Thread che si occupa del calcolo dello start point e dei periodi di off
 * @author Ambra Montecchia
 * @version 1.2
 * */
import parameters.Parameters;
import debug.DebugConsolle;

public class CBRScheduler implements IScheduler{

	//numero di frame nel buffer del client
	private int clientFrames;
	//numero di frame nel buffer del proxy
	private int proxyFrames;
	//bitrate
	private int bitrate;
	//lunghezza dei frame
	private int frameLen;
	//consolle di debug
	private DebugConsolle dc;
	//banda di trasmissione
	private double band;
	
	/**
	 * Costruttore
	 * */
	public CBRScheduler(DebugConsolle con){
		clientFrames = 0;
		proxyFrames = 0;
		//valori di default
		bitrate = 131072;	//bitrate 128Kb/s
		frameLen = 1300480;	//lunghezza media frame
		dc = con;
		band = 5662310; //bit/s
	}
	
	/**
	 * Costruttore
	 * */
	public CBRScheduler(int bRate, int fLen, DebugConsolle con){
		clientFrames = 0;
		proxyFrames = 0;
		bitrate = bRate;
		frameLen = fLen;
		dc = con;
		band = 5662310; //bit/s
	}
	
	public void setBitRate(int bRate){
		bitrate = bRate;
	}
	
	public void setClientFrames(int cFrames){
		clientFrames = cFrames;
	}
	
	public void setFrameLength(int fLen){
		frameLen = fLen;
	}
	
	public void setProxyFrames(int pFrames){
		proxyFrames = pFrames;
	}
	
	public void setBand(double b){
		band = b;
	}
	
	public double sleepTime(){
		System.out.println("Calcolo durata periodo di off:\nLunghezza frame: "+frameLen+"\nBitrate: "+bitrate+"\nNumero di frame sul client: "+clientFrames+"\nNumero di frame sul proxy: "+proxyFrames);
		double cDuration = (((double)clientFrames - (double)Parameters.SECURITY_THRESHOLD)*(double)frameLen/(double)bitrate)*1000;
		double pDuration = (((double)Parameters.PROXY_BUFFER - (double)proxyFrames)*(double)frameLen/(double)bitrate)*1000;
		System.out.println("pduration: "+pDuration+" cduration: "+cDuration);
		if(cDuration > pDuration){
			System.out.println("Durata periodo di off: "+pDuration+" ms");
			return pDuration;
		}
		else {
			System.out.println("Durata periodo di off: "+cDuration+" ms");
			return cDuration;
		}
	}
	
	public double startPlayback(){
		double delay = ((frameLen/band)*Parameters.SP_PACK)*1000;
		return delay;
	}
}
